<!-- eslint-disable vue/max-attributes-per-line -->
<template>
  <div class="department-container">
    <div class="app-container">
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="hDialogClose"
      >
        <!-- 子组件 -->
        <!-- v-if="showDialog" -->
        <deptDialog
          :id="curId"
          ref="refDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
        <!-- <AddorEdit  /> -->
      </el-dialog>
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >编辑部门</el-dropdown-item
                    >
                    <el-dropdown-item>删除部门</el-dropdown-item>
                    <el-dropdown-item />
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主题内容 -->
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :default-expand-all="false">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import deptDialog from './deptDialog.vue'
import { tranListToTreeData } from '@/utils'
// import AddorEdit from './deptDialog.vue'
export default {
  // components: { AddorEdit, deptDialog },
  components: { deptDialog },
  data() {
    return {
      originList: [],
      isEdit: false,
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hDialogClose() {
      this.$refs.refDept.resetForm()
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 保存数据

        // shift会取出数组第一个元素，同时会改变源数组
        const company = res.data.depts.shift()
        console.log('loadDepartments', company)
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            code: item.code,
            name: item.name,
            pid: item.pid
          }
        })

        this.list = tranListToTreeData(res.data.depts)
        // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
        // 为了后面计算方便，把它直接取出来
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 做删除
          this.doDel(id)
        })
        .catch(() => {})
    },
    async doDel(id) {
      try {
        const res = await delDepartments(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
      // 4. 强制让子组件去发请求获取详情
      this.$nextTick(() => {
        this.$refs.refDept.loadDetail()
      })
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 收到子组件中的close事件
    hClose() {
      this.showDialog = false
    }
  }
}
</script>
